/*
 * Tic-Tac-Toe - Turn-based strategy game
 * Copyright 2008, 2009 Shayne Riley and Paul Maseberg
 *
 * Tic-Tac-Toe is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tic-Tac-Toe is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tic-Tac-Toe.  If not, see <http://www.gnu.org/licenses/>
 * or write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * To contact the authors on questions or issues regarding the license,
 * you may send an email to <shayner at gmail dot com>
 */

#ifndef BUTTON_H_
#define BUTTON_H_

#include "Graphics.h"

/**
 * Rather than being a button that has a graphical display, it is more of a
 * clickable-region, which handles the mouse-side of the button abilities. This
 * is a polling implementation, instead of an event-based implementation, which
 * probably would have made more sense. There's always 2.0.
 */
class Button
{
public:
    /**
     * Constructs a mouse-clickable region of the display. Activity inside the
     * area that the region is defined will be listed out in the is* functions.
     * @param leftX The left side of the region, in pixels from the left side
     *  of the screen.
     * @param lowerY    The lower side of the region, in pixels from the bottom
     *  side of the screen.
     * @param width The width of the region in pixels.
     * @param height    The height of the region in pixels.
     */
    Button(int leftX = -1, int lowerY = -1, int width = 0, int height = 0);

    virtual ~Button();

    /**
     * Update the button condition with the new user input state.
     * @param mouse The user input state. Where is the mouse now, is the button
     * 	pressed, that sort of thing.
     */
    void update(const Graphics::Mouse& mouse);

    /**
     * Indicates if the mouse is located over the button, but the user isn't
     * clicking on it, and was not clicking on a region outside of the button
     * before it moved over the button.
     *
     * @return true if user mouse is hovering over button, or false if not.
     */
    bool isHovering();

    /**
     * Indicates if the user is holding the button down, but has not yet
     * released it.
     *
     * If the mouse is located over the button, and the user started
     * clicking the mouse button while inside the button. If the user started
     * clicking the mouse button while inside the button, but then moved the
     * mouse outside of the button, that would qualify as not being pressed.
     * If the user then moved the mouse back inside, while pressing the button,
     * then it returns back to being pressed.
     *
     * @return true if the button is being pressed.
     */
    bool isPressing();

    /**
     * Returns true if the button had been clicked by the user since the button
     * was last updated.
     *
     * @return true if user click the button, false if not.
     */
    bool isClicked();

protected:
    int mLeftX;
    int mLowerY;
    int mWidth;
    int mHeight;

private:
    /// The button has been clicked. This is valid for one update only.
    bool clicked;

    /// Is the mouse button pressed (started in button), but not released yet?
    bool clicking;

    /// The latest state of the mouse updated at the end of update(Mouse) call.
    Graphics::Mouse mLatestMouse; // Forget about the startup state. It's safe.

    /// @return true if the mouse is active in the game and inside the region.
    bool isMouseInRegion(const Graphics::Mouse& mouse);
};

#endif /* BUTTON_H_ */
